//
//  ShouDongDianHuoVC.m
//  DriverClient
//
//  Created by 吕晶 on 2020/8/28.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ShouDongDianHuoVC.h"
#import "ZhuangCheMainModel.h"
#import "ShouDongDianHuoCell.h"

@interface ShouDongDianHuoVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZhuangCheMainGoodModel *model;

@end

@implementation ShouDongDianHuoVC

-(ZhuangCheMainGoodModel *)model {
    if (!_model) {
        _model = [ZhuangCheMainGoodModel new];
    }
    return _model;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"手动点货";
    [self.view addSubview:self.tableView];
    if (self.clickModel) {
        self.model = self.clickModel;
        [self.mainModel.goods_list enumerateObjectsUsingBlock:^(ZhuangCheMainGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.g_id == self.clickModel.g_id) {
                if (self.clickModel.z_count) {
                    obj.z_count = self.clickModel.z_count;
                }else {
                    obj.z_count = self.clickModel.num;
                }
                if (self.clickModel.z_piece) {
                    obj.z_piece = self.clickModel.z_piece;
                }else {
                    obj.z_piece = self.clickModel.pnum;
                }
            }
        }];
        [self.tableView reloadData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShouDongDianHuoCell *cell = [ShouDongDianHuoCell ShouDongDianHuoCellWithTableView:tableView];
    cell.model = self.model;
    __weak_self;
    cell.goodClickBlock = ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [weakself.mainModel.goods_list enumerateObjectsUsingBlock:^(ZhuangCheMainGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@/%@",obj.name,obj.pro_num] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                weakself.model = obj;
                [weakself.tableView reloadData];
            }];
            [alert addAction:action1];
        }];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:cancle];
        [weakself presentViewController:alert animated:YES completion:nil];
    };
    cell.countBlock = ^(NSString * _Nonnull count) {
        weakself.model.z_count = [count intValue];
    };
    cell.pieceBlock = ^(NSString * _Nonnull piece) {
        weakself.model.z_piece = [piece intValue];
    };
    cell.luRuBlock = ^{
        //录入
        [weakself luRuClick];
    };
    return cell;
}

- (void)luRuClick {
    if (!self.model.name.length) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请选择货物"];
        return;
    }
    if (!self.model.z_count) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请填写数量"];
        return;
    }
    if (!self.model.z_piece) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请填写件数"];
        return;
    }
    if (self.finishBlock) {
        self.finishBlock();
        [self.rt_navigationController popViewControllerAnimated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShouDongDianHuoCell *cell = [ShouDongDianHuoCell ShouDongDianHuoCellWithTableView:tableView];
    cell.model = self.model;
    return cell.height;
}

@end
